package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao_Lizekai;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService_Lizekai;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service(interfaceClass = RoleService_Lizekai.class)
@Transactional
public class RoleServiceImpl_Lizekai implements RoleService_Lizekai {
    @Autowired
    private RoleDao_Lizekai roleDao;

    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.add(role);
        Integer id = role.getId();
        for (Integer permissionId : permissionIds) {
            HashMap<String, Integer> map = new HashMap<String, Integer>();
            map.put("role_id",id);
            map.put("permission_id",permissionId);
            roleDao.setPermissionIdAndRoleId(map);
        }
        for (Integer menuId : menuIds) {
            HashMap<String, Integer> map = new HashMap<String, Integer>();
            map.put("role_id",id);
            map.put("menu_id",menuId);
            roleDao.setMenuIdAndRoleId(map);
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Permission> findAll() {
        return roleDao.findAll();
    }

    @Override
    public Role findById(int id) {
        return roleDao.findById(id);
    }

    @Override
    public Result deleteById(int id) {
        int userCount = roleDao.findRoleIdAndUserId(id);
        int permissionCount =roleDao.findRoleAndPermission(id);
        int menuCount = roleDao.findRoleAndMenu(id);
        if(userCount>0){
           return new Result(false,"删除失败，角色有相关联的用户信息");
        }else if (permissionCount>0){
            return new Result(false,"删除失败，角色有相关联的权限信息");
        }else if (menuCount>0){
            return new Result(false,"删除失败，角色有相关联的菜单信息");
        }else {
            roleDao.deleteById(id);
            return new Result(true,"删除角色成功");
        }
    }

    @Override
    public void update(Role role,Integer[] permissionIds,Integer[] menuIds) {
        roleDao.EditRole(role);
        Integer roleId = role.getId();
        roleDao.deleteAssociation(roleId);
        roleDao.deleteMenuAssociation(roleId);
        for (Integer permissionId : permissionIds) {
            HashMap<String, Integer> map = new HashMap<String, Integer>();
            map.put("role_id",roleId);
            map.put("permission_id",permissionId);
            roleDao.setPermissionIdAndRoleId(map);
        }
        for (Integer menuId : menuIds) {
            HashMap<String, Integer> map = new HashMap<String, Integer>();
            map.put("role_id",roleId);
            map.put("menu_id",menuId);
            roleDao.setMenuIdAndRoleId(map);
        }
    }

    @Override
    public List<Menu> findMenuAll() {
        return roleDao.findMenuAll();
    }

    @Override
    public List<Integer> findPermissionIdByRoleId(int id) {
        return roleDao.findPermissionIdByRoleId(id);
    }

    @Override
    public List<Integer> findMenuIdByRoleId(int id) {
        return roleDao.findMenuIdByRoleId(id);
    }
}
